package com.example.AIstudy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.AIstudy.domain.*;
import com.example.AIstudy.exception.AIstudyException;
import com.example.AIstudy.service.CuoTiJiService;
import com.example.AIstudy.service.FalseReasonService;
import com.example.AIstudy.service.UserSkillScoreService;
import com.example.AIstudy.util.MyThread;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@CrossOrigin
@RequestMapping("/cuotiji")
@Api(tags = "错题集信息管理API")
public class CuoTiJiController {

    @Autowired
    CuoTiJiService cuoTiJiService;

    @Autowired
    AnalyseController analyseController;

    @Autowired
    UserSkillScoreService userSkillScoreService;

    //分页获取错题
    @ApiOperation("根据条件查询错题")
    @GetMapping("")
    public IPage<CuoTiJi> findCuoTiJi(@ApiParam("用户id")@RequestParam(value = "userID",required = false) String userID,
                                       @ApiParam("错误原因")@RequestParam(value = "falseReason",required = false) List<Integer> falseReason,
                                       @ApiParam("错题类别")@RequestParam(value = "category",required = false) List<String> category,
                                       @ApiParam("时间排序方式")@RequestParam(value = "order",required = false) String order,
                                       @ApiParam("领域标签")@RequestParam(value = "field",required = false)List<String> field,
                                       @ApiParam("页码")@RequestParam(defaultValue = "0")Integer pageNum,
                                       @ApiParam("每页记录数") @RequestParam(defaultValue = "10")Integer pageSize){

        Map<String,Object> condition=new HashMap<>();
        if(falseReason!=null) {
            condition.put("falseReason",falseReason);
        }
        if(category!=null) {
            condition.put("category",category);
        }
        if(userID!=null) {
            condition.put("userID",userID);
        }
        if(order!=null) {
            condition.put("order",order);
        }
        if(field!=null) {
            condition.put("field",field);
        }
        return cuoTiJiService.findCuoTiJi(condition, pageNum,pageSize);
    }
    @ApiOperation("根据条件查询错题")
    @GetMapping("/v2")
    public IPage<CuoTiJi> findCuoTiJiv2(@ApiParam("用户id")@RequestParam(value = "userID",required = false) String userID,
                                      @ApiParam("错误原因")@RequestParam(value = "falseReason",required = false) List<Integer> falseReason,
                                      @ApiParam("错题类别")@RequestParam(value = "category",required = false) List<String> category,
                                      @ApiParam("时间排序方式")@RequestParam(value = "order",required = false) String order,
                                      @ApiParam("领域标签")@RequestParam(value = "field",required = false)List<String> field,
                                        @ApiParam("开始时间")@RequestParam(value = "startTime",required = false)Long startTime,
                                        @ApiParam("结束时间")@RequestParam(value = "endTime",required = false)Long endTime,
                                      @ApiParam("页码")@RequestParam(defaultValue = "0")Integer pageNum,
                                      @ApiParam("每页记录数") @RequestParam(defaultValue = "10")Integer pageSize){

        Map<String,Object> condition=new HashMap<>();
        if(falseReason!=null) {
            condition.put("falseReason",falseReason);
        }
        if(category!=null) {
            condition.put("category",category);
        }
        if(userID!=null) {
            condition.put("userID",userID);
        }
        if(order!=null) {
            condition.put("order",order);
        }
        if(field!=null) {
            condition.put("field",field);
        }
        if(startTime!=null) {
            condition.put("startTime",new Timestamp(startTime));
        }
        if(endTime!=null) {
            condition.put("endTime",new Timestamp(endTime));
        }
        return cuoTiJiService.findCuoTiJi(condition, pageNum,pageSize);
    }

    @ApiOperation("根据用户ID查询错题数量")
    @GetMapping("/NumByUserID")
    public int getCuoTiNumByUserID(@ApiParam("用户ID")@RequestParam(value = "userID")Integer userID){
        return cuoTiJiService.getCuoTiNumByUserID(userID);
    }

    @ApiOperation("根据错题ID查询错题详情")
    @GetMapping("/detail")
    //    @PreAuthorize("hasAnyAuthority(#userID.toString())")
    public CuoTiJi getCuoTiDetailByID(@ApiParam("用户ID")@RequestParam(value = "userID")Integer userID,
                                      @ApiParam("错题ID")@RequestParam(value = "cuotiID")Integer cuotiID) throws AIstudyException {

        return cuoTiJiService.getCuoTiDetailByID(cuotiID);
    }


    @ApiOperation("根据错题ID更改错题批注")
    @PutMapping("/updateComment")
    //    @PreAuthorize("hasAnyAuthority(#userID.toString())")
    public void updateUserComment(@ApiParam("用户ID")@RequestParam(value = "userID")Integer userID,
                                      @ApiParam("错题ID")@RequestParam(value = "cuotiID")Integer cuotiID,
                                     @ApiParam("用户批注")@RequestParam(value = "userComment")String userComment) throws AIstudyException {

        cuoTiJiService.updateUserComment(cuotiID, userComment);
    }

    @ApiOperation("根据错题ID删除错题信息")
    @DeleteMapping("")
    //    @PreAuthorize("hasAnyAuthority(#userID.toString())")
    public void deleteCuoti(@ApiParam("用户ID")@RequestParam(value = "userID")Integer userID,
                                  @ApiParam("错题ID")@RequestParam(value = "cuotiID")Integer cuotiID) throws AIstudyException {

        cuoTiJiService.deleteCuoti(cuotiID);
    }
    @ApiOperation("获取用户错题集的全部tag")
    @GetMapping("/tagByTime")
    public Map<String, List<Object>> getTagsByTime(@ApiParam("用户ID")@RequestParam(value = "userID")Integer userID,
                                                   @ApiParam("起始时间")@RequestParam(value = "startTime",required = false)Timestamp startTime,
                                                   @ApiParam("结束时间")@RequestParam(value = "endTime",required = false)Timestamp endTime) throws AIstudyException{
        return cuoTiJiService.getTagsByTime(userID, startTime, endTime);
    }
    @ApiOperation("根据标签获取用户需要巩固的错题")
    @GetMapping("/cuotiForRedo")
    public List<CuoTiJi> getCuotiForRedo(@ApiParam("用户ID")@RequestParam(value = "userID")Integer userID,
                                         @ApiParam("类别标签")@RequestParam(value = "category",required = false)List<String> category,
                                         @ApiParam("领域标签")@RequestParam(value = "field",required = false)List<String> field,
                                         @ApiParam("错因标签")@RequestParam(value = "falseReason",required = false)List<Integer> falseReason){
        return cuoTiJiService.queryCuotiForRedo(userID, category, field, falseReason);
    }

//    @ApiOperation("根据标签获取用户需要巩固的错题")
//    @GetMapping("/cuotiForRedo")
//    public List<CuoTiJi> getCuotiForRedo(@ApiParam("用户ID")@RequestParam(value = "userID")Integer userID,
//                                         @ApiParam("类别标签")@RequestParam(value = "category",required = false)List<String> category,
//                                         @ApiParam("领域标签")@RequestParam(value = "field",required = false)List<String> field,
//                                         @ApiParam("错因标签")@RequestParam(value = "falseReason",required = false)List<Integer> falseReason){
//        return cuoTiJiService.queryCuotiForRedo(userID, category, field, falseReason);
//    }
    @ApiOperation("更新错题的标注信息")
    @PostMapping ("/updateUserComment")
    public void UpdateUserComment(@ApiParam("用户ID")@RequestParam(value = "userID")Integer userID,
                                         @ApiParam("错题的ID")@RequestParam(value = "cuotiID")Integer cuotiID,
                                         @ApiParam("错题标注")@RequestParam(value = "userComment")String userComment)throws AIstudyException{
            cuoTiJiService.updateUserComment(cuotiID,userComment);
    }

    @ApiOperation("错题的智能分析接口")
    @PostMapping ("")
    public CuoTiJi analyseCuoti(@ApiParam("错题")@RequestBody CuoTiJi cuoTiJi)throws AIstudyException{
        String category=cuoTiJi.getCategory();
        if(category.equals("yuedu")){
            List<CuoTiJi>cuoTiJis=cuoTiJi.getCuoTiList();
            int size=cuoTiJis.size();
            for(int i=0;i<size;i++){
                CuoTiJi tmpCuotiji=cuoTiJis.get(i);
                tmpCuotiji.setContent(cuoTiJi.getContent());
                tmpCuotiji.setCategory(cuoTiJi.getCategory());
                String aiComment= (String) analyseController.analyseUserAnswer(tmpCuotiji);
                tmpCuotiji.setAiComment(aiComment);
                tmpCuotiji.setContent("");
                if(tmpCuotiji.getStuAnswer().equals(tmpCuotiji.getAnswer())){
                    tmpCuotiji.setRight(true);
                    //正确后的评分
                    tmpCuotiji.setStuScore(tmpCuotiji.getScore());
                }
            }
        }else if(category.equals("zuowen")){
            CompositionDetail detail= (CompositionDetail) analyseController.analyseUserAnswer(cuoTiJi);
            cuoTiJi.setTransCommentID(detail.getId());
            cuoTiJi.setCompositionDetail(detail);
            cuoTiJi.setStuScore(detail.getTotalScore());
        }else if(category.equals("fanyi")){
            TranslateDetail detail= (TranslateDetail) analyseController.analyseUserAnswer(cuoTiJi);
            cuoTiJi.setTransCommentID(detail.getId());
            cuoTiJi.setTranslateDetail(detail);
            cuoTiJi.setStuScore(detail.getTotalScore());
        }else if(category.equals("xuanze")){
            String aiComment= (String) analyseController.analyseUserAnswer(cuoTiJi);
            cuoTiJi.setAiComment(aiComment);
            if(cuoTiJi.getStuAnswer().equals(cuoTiJi.getAnswer())){
                cuoTiJi.setRight(true);
                //正确后的评分
                cuoTiJi.setStuScore(cuoTiJi.getScore());
            }
        }
//        cuoTiJiService.insertCuoti(cuoTiJi);
        return cuoTiJi;
    }

    @ApiOperation("直接增加一道新的错题，这个增加对应的是做题后获取分析结果，用户可以点击添加进错题本")
    @PostMapping ("/addCuotiFromExe")
    public CuoTiJi inserCuotiFromExe(@ApiParam("错题")@RequestBody CuoTiJi cuoTiJi)throws AIstudyException{
        cuoTiJiService.insertCuoti(cuoTiJi);
        //开启一个新的线程来完成工作
        MyThread thread =new MyThread(userSkillScoreService);
        thread.stuID=cuoTiJi.getStuID();
        thread.start();
        return cuoTiJi;
    }
    @ApiOperation("更新错题信息，需要传递完整的题目json，和其它一致即可")
    @PutMapping ("")
    public void updateCuoti(@ApiParam("错题")@RequestBody CuoTiJi cuoTiJi)throws AIstudyException{
//        cuoTiJiService.insertCuoti(cuoTiJi);
      cuoTiJiService.updateCuoti(cuoTiJi);
    }
}
